package com.sun.dailyprj.stream;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.sun.dailyprj.R;
import com.sun.dailyprj.databinding.ActivityStreamBinding;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

/*
 * =====================================================================================
 * Summary:
 *
 * File: ActivityStream.java
 * Author: Yanpeng.Sun
 * Create: 2019/8/22 14:04
 * =====================================================================================
 */
public class ActivityStream extends Activity implements View.OnClickListener{
    ActivityStreamBinding mBinding;
    private Context mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_stream);
        mBinding.btnStreamClone.setOnClickListener(this);
        mBinding.btnStreamFd.setOnClickListener(this);

        mContext = this;

        //testStream();
        new Thread(new Runnable(){
            @Override
            public void run() {
                Log.i("syp", "thread start");
                try{
                    File f = new File("sdcard/", "test3.txt");
                    if(!f.exists()){
                        f.createNewFile();
                    }
                    InputStream stream = testBufferStream();
                    Log.i("syp", "stream.avi:" + stream);
                    FileOutputStream fos = new FileOutputStream(f);
                    if(stream != null) {
                        byte[] b = new byte[8];
                        while (stream.read(b) > 0) {
                            Log.i("syp", "b:" + b.length);
                            fos.write(b);
                        }
                        fos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

//        testInputStream();
       /* try{
            FileInputStream fis = new FileInputStream("sdcard/tap_rootfs.zip");
            Log.i("syp","avi:" + fis.available());
            FileInputStream fis2 = new FileInputStream("sdcard/dev_sdk_v3e_2019730.zip");
            Log.i("syp","avi2:" + fis2.available());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }

    private void testInputStream() {
        File file = new File("sdcard/test.txt");
        InputStream stream;
        try{
            stream = new FileInputStream(file);
            byte[] b = new byte[2];
            int len = 0;
            while ((len = stream.read(b)) > 0) {
                b = "c".getBytes();
            }
        } catch (Exception e) {
        }

    }

    private InputStream testBufferStream() {
        File file = new File("sdcard/test.txt");
        InputStream stream;
        BufferedInputStream bis;
        BufferedOutputStream bos;
        try{
            File fileDest = new File("sdcard/","test2.txt");
            if(!fileDest.exists()){
                fileDest.createNewFile();
            }
            stream = new FileInputStream(file);
            bis = new BufferedInputStream(stream);
            bos = new BufferedOutputStream(new FileOutputStream(fileDest,true));
            byte[] buffers = new byte[4];
            int available = bis.available();
            byte[] b = "cccc".getBytes();
            Log.i("syp", "len = " + buffers.length + "; b.len =" + b.length + "; available=" + available);
            Log.i("syp", "len = " + buffers.length + "; b.len =" + b.length);
//            bos.write(b);
            while (bis.read(buffers) > 0) {
                bos.write(b);
//                buffers = b;
            }
            bos.flush();
            bos.close();
            bis.close();
          /*  InputStream s = new FileInputStream(file);
            int len = s.available();
            s.skip(len/2);
            Log.i("syp","s.avi:" + s.available());*/
          FileInputStream s = new FileInputStream(fileDest);
          fileDest.delete();
            return s;
//            bos.flush();
//            bos.close();
//            bis.close();
//            InputStream s = new FileInputStream(file);
//            Log.i("syp","s:" + s.available());
//            s.read(buffers);
//            int av = s.available();
//            Log.i("syp","av:" + av);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }

    private void testStream() {
        File file = new File("sdcard/test.txt");
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try{
            fis = new FileInputStream(file);
            FileDescriptor fd = fis.getFD();
            fos = new FileOutputStream(fd);
            byte[] bytes = "cc".getBytes(Charset.defaultCharset());
            fos.write(bytes, 0, bytes.length);
            fos.flush();
            Log.i("syp", "file:" + file + " ; exist:" + file.exists() + " ; fis:" + fis.available());
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("syp", "err:" + e.toString());
        } finally {
            try{
                fis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.btn_stream_fd:

                break;
            case R.id.btn_stream_clone:
                byteClone();

                break;

        }
    }

    private void byteClone() {

        byte[] b = "cccc".getBytes();
        byte[] ba = new byte[2];
        ba = b.clone();
        Log.i("syp","ba:" + ba + " ;len:" + ba.length + ";b.len:" + b.length);

    }
}
